package com.trust.pams.dao.service.impl;

import com.chasonx.common.model.PageResult;
import com.mongodb.client.result.DeleteResult;
import com.trust.pams.dao.mongo.BaseMongoDaoImpl;
import com.trust.pams.dao.service.PamsClientDao;
import com.trust.pams.dao.service.PamsMilestoneDao;
import com.trust.pams.dto.PamsClientDto;
import com.trust.pams.dto.PamsMilestoneDto;
import com.trust.pams.model.query.PamsMilestoneQuery;
import com.trust.pams.util.DataUtil;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PamsMilestoneDaoImpl extends BaseMongoDaoImpl<PamsMilestoneDto> implements PamsMilestoneDao {
    @Override
    public int batchPamsMilestone(List<PamsMilestoneDto> pamsMilestoneDtos) {
        super.insertAll(pamsMilestoneDtos);
        return 1;
    }

    @Override
    public DeleteResult deletePamsMilestone(String projectId) {
        Query query = new Query();
        Criteria criteria = Criteria.where("projectId").is(projectId);
        query.addCriteria(criteria);
        return super.getMongoTemplate().remove(query, PamsClientDto.class);
    }

    @Override
    public List<PamsMilestoneDto> getPamsMilestone(String projectId) {
        Query dbQuery = new Query();
        dbQuery.addCriteria(Criteria.where("projectId").is(projectId));
        List<PamsMilestoneDto> listData = find(dbQuery);
        return listData;
    }

    public List<PamsMilestoneDto> getPamsMilestones(String projectId) {
        Query dbQuery = new Query();
        dbQuery.addCriteria(Criteria.where("projectId").is(projectId).and("rows.项目状态").regex("正常进行"));
        List<PamsMilestoneDto> listData = find(dbQuery);
        return listData;
    }

    public List<PamsMilestoneDto> getConditionMilestone(String projectId,String key) {
        Query dbQuery = new Query();
        dbQuery.addCriteria(Criteria.where("projectId").is(projectId).and("rows."+key).regex("正常进行"));
        List<PamsMilestoneDto> listData = find(dbQuery);
        return listData;
    }

    @Override
    public PageResult<PamsMilestoneDto> listPamsMilestone(PamsMilestoneQuery pamsMilestoneQuery) {
        Query dbQuery = new Query();
        if(DataUtil.isNotEmpty(pamsMilestoneQuery.getProjectId())){
            dbQuery.addCriteria(Criteria.where("projectId").is(pamsMilestoneQuery.getProjectId()));
        }
        long count = getMongoTemplate().count(dbQuery, getCollectionName());
        dbQuery.skip( (pamsMilestoneQuery.getPageNumber() - 1) * pamsMilestoneQuery.getPageSize()).limit(pamsMilestoneQuery.getPageSize());

        List<PamsMilestoneDto> listData = find(dbQuery);

        PageResult<PamsMilestoneDto> page = new PageResult<>();
        page.setTotalRow(count);
        page.setData(listData);
        return page;
    }
}
